home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJSRC111.ZIP / go32 / xms.c < prev    next >
C/C++ Source or Header  |  1993-07-04  |  6KB  |  322 lines

  1. /*
  2. ** xms.c -- C bindings for xms API - implemented
  3. ** Author: Kent Williams william@umaxc.weeg.uiowa.edu
  4. */
  5. #pragma inline
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "xms.h"
  9.  
  10. static void far xms_spoof(void);
  11. static void (far *xms_entry)(void) = xms_spoof;
  12. char xms_error;
  13.  
  14. int
  15. xms_installed(void) {
  16.     asm    mov    ax,0x4300    /* get installation status */
  17.     asm    int 0x2f        /* call driver */
  18.     asm    mov    ah,1        /* return true if present */
  19.     asm    cmp al,0x80        /* are you there? */
  20.     asm    je __present
  21.     asm    xor ax,ax        /* no, return false */
  22.     __present:
  23.     asm    xchg    ah,al    /* if present, set ax to 1 */
  24.     asm    cbw
  25.     return    _AX;
  26. }
  27.  
  28. static void
  29. xms_get_entry(void) {
  30.     asm    mov    ax,0x4310    /* get driver entry */
  31.     asm    int 0x2f        /* call multiplex */
  32.     asm    mov word ptr xms_entry,bx
  33.     asm    mov    word ptr (xms_entry+2),es
  34. }
  35.  
  36. static void far
  37. xms_spoof(void) {
  38.     asm    push ax
  39.     asm    push bx
  40.     asm    push cx
  41.     asm    push dx
  42.     asm    push si
  43.     asm    push di
  44.     asm    push es
  45.     asm    push ds
  46.     if(!xms_installed()) {
  47.         fputs("No XMS driver installed\n",stderr);
  48.         exit(1);
  49.     } else
  50.         xms_get_entry();
  51.     asm    pop ds
  52.     asm    pop    es
  53.     asm    pop    di
  54.     asm    pop    si
  55.     asm    pop    dx
  56.     asm    pop cx
  57.     asm    pop    bx
  58.     asm    pop    ax
  59.     xms_entry();
  60. }
  61.  
  62. xms_version_info *
  63. xms_get_version_info(void) {
  64.     unsigned xms_ver,xmm_ver,hma_indicator;
  65.     static xms_version_info x;
  66.     asm    xor ah,ah
  67.     asm    call [xms_entry]
  68.     asm    mov    xms_error,bl
  69.     asm    mov    xms_ver,ax
  70.     asm    mov xmm_ver,bx
  71.     asm    mov    hma_indicator,dx
  72.     xms_ver = (xms_ver & 0xf) + (((xms_ver >> 4) & 0xf) * 10) +
  73.             ((xms_ver >> 8) & 0xf) * 100 + ((xms_ver >> 12) & 0xf) * 1000;
  74.  
  75.     xmm_ver = (xmm_ver & 0xf) + (((xmm_ver >> 4) & 0xf) * 10) +
  76.             ((xmm_ver >> 8) & 0xf) * 100 + ((xmm_ver >> 12) & 0xf) * 1000;
  77.     x.xms_ver = xms_ver; x.xmm_ver = xmm_ver; x.hma_present = hma_indicator;
  78.     return &x;
  79. }
  80.  
  81. xms_extended_info *
  82. xms_query_extended_memory(void) {
  83.     unsigned max_free_block,total_extended_memory;
  84.     static xms_extended_info x;
  85.  
  86.     asm    mov    ah,0x8
  87.     asm    call [xms_entry]
  88.     asm    mov    xms_error,bl
  89.     asm    mov    max_free_block,ax
  90.     asm    mov    total_extended_memory,dx
  91.     x.max_free_block = max_free_block;
  92.     x.total_extended_memory = total_extended_memory;
  93.     return &x;
  94. }
  95.  
  96. int
  97. xms_hma_allocate(unsigned siz) {
  98.     asm    mov    ah,0x01
  99.     asm    mov    dx,word ptr siz
  100.     asm    call [xms_entry]
  101.     asm    mov    xms_error,bl
  102.         /* xms returns 1 on success, 0 on failure.  We need to invert this
  103.         ** for the C idiom.
  104.         ** if ax = 1 then not ax = 0xfffe and add ax,2 = 0
  105.         ** if ax = 0 then not ax = 0xffff and add ax,2 = 1
  106.         */
  107.     asm    not ax
  108.     asm    inc ax
  109.     asm    inc ax
  110.     return    _AX;
  111. }
  112.  
  113. int
  114. xms_hma_free(void) {
  115.     asm    mov    ah,0x02
  116.     asm    call [xms_entry]
  117.     asm    mov    xms_error,bl
  118.     asm    not ax
  119.     asm    inc ax
  120.     asm    inc ax
  121.     return    _AX;
  122. }
  123.  
  124.  
  125. int
  126. xms_global_enable_a20(void) {
  127.     asm    mov    ah,0x03
  128.     asm    call [xms_entry]
  129.     asm    mov    xms_error,bl
  130.     asm    not ax
  131.     asm    inc ax
  132.     asm    inc ax
  133.     return    _AX;
  134. }
  135.  
  136. int
  137. xms_global_disable_a20(void) {
  138.     asm    mov    ah,0x4
  139.     asm    call [xms_entry]
  140.     asm    mov    xms_error,bl
  141.     asm    not ax
  142.     asm    inc ax
  143.     asm    inc ax
  144.     return    _AX;
  145. }
  146.  
  147. int
  148. xms_local_enable_a20(void) {
  149.     asm    mov    ah,0x5
  150.     asm    call [xms_entry]
  151.     asm    mov    xms_error,bl
  152.     asm    not ax
  153.     asm    inc ax
  154.     asm    inc ax
  155.     return    _AX;
  156. }
  157.  
  158. int
  159. xms_local_disable_a20(void) {
  160.     asm    mov    ah,0x6
  161.     asm    call [xms_entry]
  162.     asm    mov    xms_error,bl
  163.     asm    not ax
  164.     asm    inc ax
  165.     asm    inc ax
  166.     return    _AX;
  167. }
  168.  
  169.  
  170. int
  171. xms_a20_status(void) {
  172.     asm    mov    ah,0x7
  173.     asm    call [xms_entry]
  174.     asm    mov    xms_error,bl
  175.     return    _AX;
  176. }
  177.  
  178. int
  179. xms_emb_allocate(emb_size_K_t siz) {
  180.     asm    mov ah,0x9
  181.     asm    mov    dx,siz
  182.     asm    call [xms_entry]
  183.     asm    mov    xms_error,bl
  184.     asm    or    ax,ax            /* allocation succeed? */
  185.     asm    jz alloc_failed        /* no, return 0           */
  186.     asm    xchg ax,dx            /* yes, return handle */
  187.     asm    jmp done
  188. alloc_failed:
  189.     asm    mov    ax,-1
  190. done:;
  191.     return    _AX;
  192. }
  193.  
  194. int
  195. xms_emb_free(emb_handle_t handle) {
  196.     asm    mov    ah,0x0a
  197.     asm    mov dx,handle
  198.     asm    call [xms_entry]
  199.     asm    mov    xms_error,bl
  200.     asm    not ax
  201.     asm    inc ax
  202.     asm    inc ax
  203.     return    _AX;
  204. }
  205.  
  206. emb_handle_info *
  207. xms_get_emb_handle_info(emb_handle_t handle) {
  208.     static emb_handle_info info;
  209.     unsigned char locks,handles;
  210.     emb_size_K_t siz;
  211.     asm    mov    ah,0x0e
  212.     asm    mov    dx,handle
  213.     asm    call [xms_entry]
  214.     asm    or ax,ax        /* did call fail? */
  215.     asm    jz call_failed
  216.     asm    mov    locks,bh
  217.     asm    mov handles,bl
  218.     asm    mov    siz,dx
  219.     asm    jmp short done
  220.     call_failed:
  221.     asm    mov    xms_error,bl
  222.     asm    mov ax,1
  223.     asm    mov word ptr siz,-1
  224.     done:
  225.     info.lock_count = locks;
  226.     info.handles_available = handles;
  227.     info.block_size = siz;
  228.     return siz == 0xffff ? NULL : &info;
  229. }
  230.  
  231. int
  232. xms_emb_resize(emb_handle_t handle,emb_size_K_t siz) {
  233.     asm    mov    ah,0xf
  234.     asm    mov    bx,siz
  235.     asm    mov    dx,handle
  236.     asm    call [xms_entry]
  237.     asm    mov    xms_error,bl
  238.     asm    not ax
  239.     asm    inc ax
  240.     asm    inc ax
  241.     return    _AX;
  242. }
  243.  
  244. int
  245. xms_move_emb(xms_move_param *x) {
  246.     xms_move_param far *x2 = (xms_move_param far *)x;
  247.     asm    push    ds
  248.     asm    mov    ah,0xb
  249.     asm    lds    si,x2
  250.     asm    call [xms_entry]
  251.     asm    mov    xms_error,bl
  252.     asm    not ax
  253.     asm    inc ax
  254.     asm    inc ax
  255.     return    _AX;
  256. }
  257.  
  258. emb_off_t
  259. xms_lock_emb(emb_handle_t handle) {
  260.     asm    mov    ah,0x0c
  261.     asm    mov    dx,handle
  262.     asm    call [xms_entry]
  263.     asm    cmp    ax,0
  264.     asm    je    lock_failed
  265.     asm    mov    ax,bx        /* return value in dx:ax */
  266.     asm    mov    xms_error,0
  267.     asm    jmp    lock_done
  268. lock_failed:
  269.     asm    mov    dx,ax
  270.     asm    mov    xms_error,bl
  271. lock_done:
  272.     ;
  273.     asm pop bp    /* these do the actual returning, */
  274.     asm ret        /* the return below is to satisfy tcc */
  275.  
  276.     return 0;    /* we can't tell tcc that we filled DX:AX for
  277.                return, so we fake it to silence the warning */
  278. }
  279.  
  280. int
  281. xms_unlock_emb(emb_handle_t handle) {
  282.     asm    mov    ah,0x0d
  283.     asm    mov    dx,handle
  284.     asm    call [xms_entry]
  285.     asm    mov    xms_error,bl
  286.     asm    not ax
  287.     asm    inc ax
  288.     asm    inc ax
  289.     return _AX;
  290. }
  291.  
  292. umb_info *
  293. xms_umb_allocate(umb_size_t siz) {
  294.     static umb_info x;
  295.     unsigned segment,block_size;
  296.     asm    mov    ah,0x10
  297.     asm    mov    dx,siz
  298.     asm    call [xms_entry]
  299.     asm    mov xms_error,bl
  300.     asm    mov segment,bx
  301.     asm    mov    block_size,dx
  302.     asm    or ax,ax
  303.     asm    jnz done
  304.     asm    mov    segment,ax
  305. done:
  306.     x.segment = segment;
  307.     x.block_size = block_size;
  308.     return segment != 0 ? &x : NULL;
  309. }
  310.  
  311. int
  312. xms_umb_free(umb_segment_t segment) {
  313.     asm    mov    ah,0x11
  314.     asm    mov    dx,segment
  315.     asm    call [xms_entry];
  316.     asm    mov    xms_error,bl
  317.     asm    not ax
  318.     asm    inc ax
  319.     asm    inc ax
  320.     return _AX;
  321. }
  322.